$(function () {
    var allElection = $('#allElection');//全选按钮
    var reverseElection = $('#reverseElection');//反选按钮
    var Del = $('.Del');//删除按钮
    var oTable = $('.Table-Size');
    var addName=$('#name');
    var addbthDay=$('#bthDay');
    var addPost=$('#post');
    var addLevel=$('#level');
    var addJob=$('#Job');
    var addTchcu=$('#tcHcu');
    var addOtime=$('#oTime');
    var addGrowth_Archives=$('#Growth_Archives');


    var mtfName=$('#name2');
    var mtfPost=$('#post2');
    var mtfLevel=$('#level2');
    var mtftcHcu=$('#tcHcu2');

    var firLoadPages = 1;
    var addTeacherBtn=$('#addTeacher');//添加教师按钮
    var addData=$('#Add');//弹框里面的添加按钮
    var delall=$('.del-all')
    var infor=$('.modify-infor>li>input[type = text]');//修改框里面的输入框
    var aNext = $('.next');
    var aPre = $('.pre');
    var n=1;
    // var aaa;
    verify(addName,/^[\u4E00-\u9FA5A-Za-z]+$/,'姓名只能为中文和英文',3,false);
    verify(addbthDay,/^[\d]{4}[-\ ][\d]{1,2}[-\ ][\d]{1,2}$/,'出生日期必须按照yyyy-mm-dd格式',5,false);
    verify(addPost,/\u4e00-\u9fa5{2,8}/,'职务必须为2至8位中文',6,false);
    verify(addJob,/^[A-Z][0|1]20([0|1])[0-9](0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0|1])$/,'工号',8,false);
    verify(addLevel,/\u4e00-\u9fa5{2,8}/,'级别必须为2至8位中文',7,false);
    verify(addTchcu,/\u4e00-\u9fa5{2,8}/,'课程必须为2至8位中文',9,false);
    verify(addOtime,/^[\d]{4}[-\ ][\d]{1,2}[-\ ][\d]{1,2}$/,'入职时间必须按照yyyy-mm-dd格式',10,false);
    verify(addGrowth_Archives,/^[\u4E00-\u9FA5]{1,20}$/,'个人简介为1-20个文字',11,false)


    verify(mtfName,/^[\u4E00-\u9FA5A-Za-z]+$/,'姓名只能为中文和英文',3,false);
    verify(mtfPost,/\u4e00-\u9fa5{2,8}/,'职务必须为2至8位中文',6,false);
    verify(mtfLevel,/\u4e00-\u9fa5{2,8}/,'级别必须为2至8位中文',7,false);
    verify(mtftcHcu,/\u4e00-\u9fa5{2,8}/,'课程必须为2至8位中文',9,false);
        // var arri = JSON.parse(localStorage.getItem('teaInform'));
        var arr=JSON.parse(localStorage.getItem('teaInform'))||[];
        // var tableNameArr = ['teaInform'];
     /*   function setLocalStorage(m){
            $.each(m,function(i,n){
                localStorage.setItem(tableNameArr[i],JSON.stringify(n));
            })
        };

      setLocalStorage(arr);*/

    function render () {
        oTable.find('tbody').html('');
        //遍历创建元素并放入信息
        var Slicearr=paging(n);
        $.each(Slicearr,function (i) {

            var otd=$('<tr class="tbody-tr">' +
                '<td class="S Item">' +
                '<input type="checkbox" class="checkbtn">' +
                '</td>' +
                '<td class="M">'+(i+1)+'</td>' +
                '<td class="M">'+Slicearr[i].name+'</td>' +
                '<td class="M">'+Slicearr[i].sex+'</td>' +
                '<td class="B">'+Slicearr[i].bthDay+'</td>' +
                '<td class="M">'+Slicearr[i].post+'</td>' +
                '<td class="M">'+Slicearr[i].level+'</td>' +
                '<td class="M">'+Slicearr[i].Job+'</td>' +
                '<td class="B">'+Slicearr[i].tcHcu+'</td>' +
                '<td class="B">'+Slicearr[i].oTime+'</td>' +
                '<td class="B">'+Slicearr[i].Growth_Archives+'</td>' +
                '<td class="B">' +
                '<span class="content-button"><i class="fa fa-pencil-square-o Modify" data-toggle="modal" data-target="#myModal2"></i></span>' +
                '<span class="content-button"><i class="fa fa-trash btn-Del" aria-hidden="true" data-toggle="modal" data-target="#myModal3"></i></span>' +
                '</td>'+
                '</tr>');
            // render();
            //将元素插入入表格
            $('.Table').append(otd);
            //重新排序
            tableN();

        });
        localStorage.setItem('teaInform',JSON.stringify(arr));
    };

    if(arr.length){
        render();
    }

    //全选
    $("#allElection").on('click', function() {
        if($("tbody input:checkbox").length===$("tbody input:checked").length) {
            $("tbody input:checkbox").prop('checked',false);
        }else {
            $("tbody input:checkbox").prop("checked",true);
        }
    })

    //反选
    $('#reverseElection').on('click',function(){

        $("tbody input:checkbox").each(function () {
            $(this).prop("checked", !$(this).is(':checked'));
        });
    })

   // 单项删除
   oTable.on('click','.btn-Del',function () {
        var pre = $(this).parents('tr').children(':nth-child(2)').text();
        arr.splice(pre-1,1);
    })
    $('.btn-del').on('click',function(){
        render();
    });




    //多选删除
    delall.on('click',function(){
        var checkL = $(" input[class='checkbtn']");
        for(var i=checkL.length-1;i>=0;i--){
            if(checkL[i].checked == true){
                arr.splice(i,1);
            }
        }

        $('.btn-del').on('click',function(){
            render();
        });
    });




    //添加
    function tableN() {
        var aTableN=$('.table-tr>td:nth-of-type(2)');
        aTableN.each(function () {
            $(this).text($(this).parent().index())
        })
    }

    // console.log(arr)

    addData.on('click',function () {
        var rule1=verify(addName,/^[\u4E00-\u9FA5A-Za-z]+$/,'姓名只能为中文和英文',3,false);
        var rule2=verify(addbthDay,/^[\d]{4}[-\ ][\d]{1,2}[-\ ][\d]{1,2}$/,'出生日期必须按照yyyy-mm-dd格式',5,false);
        var rule3=verify(addPost,/\u4e00-\u9fa5{2,8}/,'职务必须为2至8位中文',6,false);
        var rule4=verify(addJob,/^[A-Z][0|1]20([0|1])[0-9](0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0|1])$/,'工号',8,false);
        var rule5=verify(addLevel,/\u4e00-\u9fa5{2,8}/,'级别必须为2至8位中文',7,false);
        var rule6=verify(addTchcu,/\u4e00-\u9fa5{2,8}/,'课程必须为2至8位中文',9,false);
        var rule7=verify(addOtime,/^[\d]{4}[-\ ][\d]{1,2}[-\ ][\d]{1,2}$/,'入职时间必须按照yyyy-mm-dd格式',10,false);
        var rule8=verify(addGrowth_Archives,/^[\u4E00-\u9FA5]{1,20}$/,'个人简介为1-20个文字',11,false);
        if(rule1||rule2||rule3||rule4||rule5||rule6||rule7||rule8){
            arr.push({
                name:addName.val(),
                bthDay:addbthDay.val(),
                post:addPost.val(),
                level:addLevel.val(),
                Job:addJob.val(),
                tcHcu:addTchcu.val(),
                oTime:addOtime.val(),
                Growth_Archives:addGrowth_Archives.val()
            });
            render();
            alert('添加成功！');
        }
        // alert('添加成功');
    });
    function verify(Samet,reg,str,n,tf){
        var res;
        var arry = [];

        for(var i=0;i<$('.Table tbody>tr>td:nth-child('+n+')').length;i++){
            if(tf){
                Samet.val() == $('.Table tbody>tr>td:nth-child('+n+')').eq(i).text();
                i++;
            }
            arry.push($('.Table tbody>tr>td:nth-child('+n+')').eq(i).text())
        }
        Samet.focusout(res = function(){
            if(!reg.test(Samet.val())){
                Samet.next().text(str);
                return  false;
            }else if(arry.indexOf(Samet.val())!=-1){
                Samet.next().text('命名冲突');
                return  false;
            }
            else {
                Samet.next().text('');
                return true;
            }
        });
        return res();
    };




    //序号
    $(function(){
        //$('table tr:not(:first)').remove();
        var len = $('table tr').length;
        for(var i = 1;i<len;i++){
            $('table tr:eq('+i+') td:nth-child(2)').text(i);
        }

1    });

    $('#Add').click(function () {
        var sex='男';
        if($('#women').prop('checked')){sex='女'}
        var newStu={
            name:$('#name').val(),
            sex:sex,
            bthDay:$('#bthDay').val(),
            post:$('#post').val(),
            level:$('#level').val(),
            Job:$('#Job').val(),
            tcHcu:$('#tcHcu').val(),
            oTime:$('#oTime').val(),
            Growth_Archives:$('#Growth_Archives').val()
        };
        arr.push(newStu);//将信息存入数组
        // $('.tbody-tr').nextAll('tr').remove();
        render();
    });



    //修改数据
    var thisTr
    oTable.on('click','.Modify',function(){
        thisTr = $(this).parents('tr').index();
        $.each(infor,function (index,element) {
            infor[index].value =arr[thisTr][infor[index].name];
        })
    });
        // console.log(infor)
    var valueSamet;
    var indexI;
    var inPUT;
        $('.modify-infor').on('click','i',function(){
            // $(this).next().removeAttr('disabled').removeAttr('placeholder');

             valueSamet = $(this).prev().val();
             indexI = $(this).parents().index();
             inPUT=$('.Infor');

        });
    infor.on('focusout',function(){
        arr[thisTr][infor[indexI].name] = $(this).val();
        // console.log( arr[thisTr][infor[indexI].name])

        /*arr.each(function (i,v) {

        })*/
    })

    $('.modify-Preser').on('click',function () {
       /* var ruler1=verify(mtfName,/^[\u4E00-\u9FA5A-Za-z]+$/,'姓名只能为中文和英文',3,false);
        var ruler2=verify(mtfPost,/\u4e00-\u9fa5{2,8}/,'职务必须为2至8位中文',6,false);
        var ruler3=verify(mtfLevel,/\u4e00-\u9fa5{2,8}/,'级别必须为2至8位中文',7,false);
        var ruler4=verify(mtftcHcu,/\u4e00-\u9fa5{2,8}/,'课程必须为2至8位中文',9,false);*/
        if (true){
            render();
        }
    })


    //判断页数
    function showPage(){
        var PageCount = Math.ceil(arr.length/6);
        $('.Pag-List').html('');
        for(var i=1;i<=PageCount;i++){
            $('<li class="PageNum"><a href="#" >'+i+'</a>').appendTo($('.Pag-List'));
            if(i==1){
            	$('.PageNum').eq(0).addClass('active');
            }
        }

    }
    showPage();

    function paging (n) {
        return arr.slice((n-1)*6,6*n)
    }

    $('.Pag-List').on('click','li',function () {
        var g=$(this).text();
        n=parseInt(g);
        $(this).addClass('active').siblings('li').removeClass('active');
        render();
    })
 /*   function getPageDate(index){
        aaa=classData.slice((index-1)*6,6*index);
        render()
    }
    getPageDate(1)
*/

   // 搜索
    $(function() {
        var search_input = $(".input-Box");
         var search_content = $(".bodyyy");
        $(search_input).on("keyup", function() {
            if (search_input.val() == '') {
                $(arr.value).show();
            }
            $(".search_content tr:contains(" + search_input.val().trim() + ")").show();
            $(".search_content tr:not(:contains(" + search_input.val().trim() + "))").hide();

        });
    });













































})




